import java.util.Map;
import java.util.TreeMap;

public class Solution826 {
    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        TreeMap<Integer,Integer> treeMap=new TreeMap<>();
        for (int i=0;i<difficulty.length;i++){
            int t=profit[i];
            if (treeMap.containsKey(difficulty[i])){
                t=Math.max(t,treeMap.get(difficulty[i]));
            }
            treeMap.put(difficulty[i],t);
        }
        int max=Integer.MIN_VALUE;
        for (Map.Entry<Integer, Integer> entry : treeMap.entrySet()) {
            max=Math.max(max,entry.getValue());
            treeMap.put(entry.getKey(),max);
        }
        int ans=0;
        for (int i : worker) {
            ans+=treeMap.floorKey(i)==null?0:treeMap.get(treeMap.floorKey(i));
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution826().maxProfitAssignment(new int[]{2,4,6,8,10},new int[]{10,20,30,40,50},new int[]{4,5,6,7}));
    }
}
